package com.iedu.demo.lgq.controller;

import com.iedu.demo.lgq.entity.TbUserFavorites;
import com.iedu.demo.lgq.service.UserFavoritesService;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("logic/favorites")
@CrossOrigin
public class UserFavoritesController {

    @Autowired
    private UserFavoritesService userFavoritesService;

    // 添加商品到收藏夹
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public SaResult addToFavorites(@RequestParam String itemId) {
        String userName = StpUtil.getLoginIdAsString(); // 获取当前登录的用户ID
        userFavoritesService.addToFavorites(userName, itemId);
        return SaResult.code(200).setMsg("商品已添加到收藏夹");
    }

    // 获取当前用户的收藏夹商品
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public SaResult getFavorites() {
        String userName = StpUtil.getLoginIdAsString(); // 获取当前登录的用户ID
        List<TbUserFavorites> favoritesList = userFavoritesService.findFavoritesByUserId(userName);
        return SaResult.code(200).setData(favoritesList);
    }

    // 从收藏夹中删除商品
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public SaResult removeFromFavorites(@RequestParam String itemId) {
        String userName = StpUtil.getLoginIdAsString(); // 获取当前登录的用户ID
        userFavoritesService.removeFromFavorites(userName, itemId);
        return SaResult.code(200).setMsg("商品已从收藏夹移除");
    }
}
